<?php
require_once("../Scripts/authenticate.php"); 
require_once("DatabaseConnect.php"); 

//switch based on submit request
switch ($_REQUEST['submit']){
	
	//If update entry button is pressed:
	case "Update Entry":
		//Obtain data needed from the form
		$Email=$_SESSION['email'];
		$Type=$_POST['Type'];
		echo"Type: $Type";
		$Date=$_POST['Date'];
		$ID=$_POST['ID'];
		$Notes=$_POST['Notes'];	

		
		//Database connection
		$con=Remoteconnect();
		if($con==null){
			$con=Localconnect();	
		}
				
		if($Type=='buy'){
			
			//Data that is not used in cash Deposit/Withdraw tranactions:
			$Shares=$_POST['Shares'];
			$Price=$_POST['Price'];
			$Commission=$_POST['Commission'];			
			$CashValue=$Price*$Shares+$Commission;
			if(isset($_POST['LinkCash'])){
				$LinkCash="yes";	
			}else{
				$LinkCash="no";
			}
			
			//If the transaction is linked to the user cash pool
			if($LinkCash=="yes"){
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
			
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			
			}
			//Do the calculation	
			$Cash=$Cash+$oldCashValue;
			$Cash=$Cash-$CashValue;
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
			}
			
			//Update the users portfolio with the new data from the form
			$sql =mysqli_query($con,"UPDATE `userportfolio` SET `Type` = '$Type', `Date` = '$Date', `Shares` = '$Shares', `Price` = '$Price', `Commission` ='$Commission', `Notes` = '$Notes', `CashValue`='$CashValue', `LinkCash`='$LinkCash' WHERE `ID` = '$ID'");
			
		}

		if($Type=='Withdraw'){
			//Get transaction amount from edit data page
			$CashValue=$_POST['Amount'];
				
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
			
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			
			}
			//Do the calculation	
			$Cash=$Cash+$oldCashValue;
			$Cash=$Cash-$CashValue;
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
			
			
			//Update the users portfolio with the new data from the form
			$sql =mysqli_query($con,"UPDATE `userportfolio` SET `Type` = '$Type', `Date` = '$Date', `Notes` = '$Notes', `CashValue`='$CashValue' WHERE `ID` = '$ID'");
			
		}
		
		if($Type=='sell'){
			//Data that is not used in cash Deposit/Withdraw tranactions:
			$Shares=$_POST['Shares'];
			$Price=$_POST['Price'];
			$Commission=$_POST['Commission'];			
			$CashValue=$Price*$Shares-$Commission;
			if(isset($_POST['LinkCash'])){
				$LinkCash="yes";	
			}else{
				$LinkCash="no";
			}
			
			//If the transaction is linked to the user cash pool
			if($LinkCash=="yes"){
			
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
			
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			
			}
			//Do the calculation	
			$Cash=$Cash-$oldCashValue;
			$Cash=$Cash+$CashValue;
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
			}
			
			//Update the users portfolio with the new data from the form
			$sql =mysqli_query($con,"UPDATE `userportfolio` SET `Type` = '$Type', `Date` = '$Date', `Shares` = '$Shares', `Price` = '$Price', `Commission` ='$Commission', `Notes` = '$Notes', `CashValue`='$CashValue', `LinkCash`='$LinkCash' WHERE `ID` = '$ID'");
		}
		
		if($Type=='Deposit'){
			//Get transaction amount from edit data page
			$CashValue=$_POST['Amount'];
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
			
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			
			}
			//Do the calculation	
			
			$Cash=$Cash-$oldCashValue;
			$Cash=$Cash+$CashValue;
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
			
			
			//Update the users portfolio with the new data from the form
			$sql =mysqli_query($con,"UPDATE `userportfolio` SET `Type` = '$Type', `Date` ='$Date', `Notes`='$Notes', `CashValue`='$CashValue' WHERE `ID` = '$ID'");
		}
		
		mysqli_close($con);
		header("Location: /Demo/EditData.php");
		break;
	
	//If delete button is pressed:
	case "Delete Entry":
	
		//Database connection
		$con=Remoteconnect();
		if($con==null){
			$con=Localconnect();	
		}
		//Obtain data needed from the form
		$Email=$_SESSION['email'];
		$Type=$_POST['Type'];
		$ID=$_POST['ID'];
		
		
		
		if($Type=='sell'){
			
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {	
				$oldCashValue=$row['CashValue'];
			}
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			}
			
			//Do the calculation	
			$Stop=false;
			if($Cash>0){
				$Cash=$Cash-$oldCashValue;
			$Stop=true;
			}
			if($Cash<0&$Stop==true){
				$Cash=$Cash+$oldCashValue;
			}
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
		
		
			//Delete the transaction from the users portfolio
			$result = mysqli_query($con,"DELETE FROM `userportfolio` WHERE `ID`='$ID'");
		}
		if($Type=='Deposit'){
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {	
				$oldCashValue=$row['CashValue'];
			}
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$Cash=$row['Cash'];
			}
			
			//Do the calculation	
			$Stop=false;
			if($Cash>0){
				$Cash=$Cash-$oldCashValue;
				
				$Stop=true;
			}
			if($Cash<0&$Stop==true){
				$Cash=$Cash+$oldCashValue;
			}
			
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
		
		
			//Delete the transaction from the users portfolio
			$result = mysqli_query($con,"DELETE FROM `userportfolio` WHERE `ID`='$ID'");
		}
		
		if($Type=='buy'){
			
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {;
				$Cash=$row['Cash'];
			}
			
			//Do the calculation	
			$Cash=$Cash+$oldCashValue;
			
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
		
		
			//Delete the transaction from the users portfolio
			$result = mysqli_query($con,"DELETE FROM `userportfolio` WHERE `ID`='$ID'");
		}
		if($Type=='Withdraw'){
			//Get old cash value of transaction
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio` WHERE `Email`='$Email' AND `ID`='$ID'");
			
			while ($row = mysqli_fetch_array($sql)) {
				$oldCashValue=$row['CashValue'];
			}
			
			//Get Value of Cash pool
			$sql=mysqli_query($con,"SELECT * FROM `userportfolio cash` WHERE `Email`='$Email'");
			
			while ($row = mysqli_fetch_array($sql)) {;
				$Cash=$row['Cash'];
			}
			
			//Do the calculation	
			
			$Cash=$Cash+$oldCashValue;
			
			
			//Update the users cash pool with the new amount
			$sql =mysqli_query($con,"UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE `Email`='$Email'");
		
		
			//Delete the transaction from the users portfolio
			$result = mysqli_query($con,"DELETE FROM `userportfolio` WHERE `ID`='$ID'");
		}		
		
		
		mysqli_close($con);
		header("Location: /Demo/EditData.php");
		break;
}
?>